package cn.com.ylpw.web.util;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ylpw.utils.JsonUtils;
import cn.com.ylpw.web.model.seat.SeatJudge;
import cn.com.ylpw.web.model.seat.SeatResponse;
import cn.com.ylpw.web.model.seat.SeatResult;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @description 
 * 功能描述: 前台所有系统常量
 * @author 		  作         者: 张嘉杰
 * @createdate   建立日期：Jun 9, 2013 2:56:11 PM
 * @projectname  项目名称: ylpwweb
 * @packageclass 包及类名: cn.com.ylpw.common.Constants.java
 */
@SuppressWarnings({"unchecked","static-access","deprecation","finally"})
public abstract class Constants {
	
	public static final Logger log = LoggerFactory.getLogger(Constants.class);
	
	/** 路径相关  ---------------------------------------------------------------------------------------------- */
	/* 项目class的根目录 [tomcat和console都可以取到正确的项目路径] */
	public static final String ROOT_CLASS_PATH = Constants.class.getResource("").getFile().replaceAll("(.*)classes(.*)", "$1classes/");
	/* 项目根目录 */
	public static final String ROOT_PATH = ROOT_CLASS_PATH.split("/WEB-INF")[0];
	/* 项目目录 */
	public static final String PATH = ROOT_PATH.substring(ROOT_PATH.lastIndexOf("/"), ROOT_PATH.length());
	
	/** 用户相关  ---------------------------------------------------------------------------------------------- */
	
	/** 工具相关  ---------------------------------------------------------------------------------------------- */
	/* BASE64默认ESDKEY加密的KEY */
	public static final String BASE64_DEFAULT_ESDKEY = "ylpw228!@#$%^&*()_";
	/* BASE64在线选座ESDKEY加密的KEY */
	public static final String BASE64_ONLINESEAT_ESDKEY = "v4#g%L[.";
	
	/* BASE64在线选座ESDKEY加密的KEY */
	public static final String BASE64_WANGPIAO_ESDKEY = "E2tPkfwK9bg8R8pw";
	
	/* ajax 响应  */
	public static final int RESPONSE_SUCCESS 	= 1;		// 响应成功
	public static final int RESPONSE_FAIL 		= 0;		// 响应失败
	
	/* 上传的文件大小限制 */
	public static final long FILE_SIZE = 1024 * 1024 * 2L;							// 图片上传限制2M
	public static final String FILE_TYPE_IMAGE = ".gif,.jpg,.jpeg,.png,.bmp";		// 图片类型限制
	public static final String FILE_TYPE_PATH  = "/upload/";						// 图片的上传路径
	/* 在线选座配置文件 */
	public static final PropertiesUtils OnlineSeatproperties = new PropertiesUtils("app.properties");
	/* 第三方电子票配置文件 */
	public static final PropertiesUtils DzSeatproperties = new PropertiesUtils("dzseat.properties");
	/* 大剧院在线选座配置文件 */
	public static final PropertiesUtils TheaterOnlineSeatproperties = new PropertiesUtils("theater.properties");
	/* 开心麻花在线选座配置文件 */
	public static final PropertiesUtils KaixinguoOnlineSeatproperties = new PropertiesUtils("kaixinguo.properties");
	/**
	 * @description 
	 * 功能描述: 获取选座地址
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 无
	 * @createdate   建立日期：Jul 24, 20136:00:48 PM
	 */
	public enum OnlineSeat {
		/** FALSH跳转地址  */
		FALSHURL("falshURL"),
		/** FALSH抢座跳转地址  */
		FALSHQZURL("falshQZURL"),
		/** 电子票特殊流程FALSH跳转地址  */
		FALSHDZURL("falshDZURl"),
		/** 电子票接口返回随机座位  */
		RANDOMSEATURL("randomSeatURL"),
		/** 选座服务器 获取场次URL  */
		SCREENINGSURL("screeningsURL"),
		/** 选座服务器 提交锁坐URL */
		LOCKSITURL("locksitURL"),
		/** 订单确认支付 最终锁死URL */
		SUCCESSURL("successURL"),
		/** 一次性锁座URL */
		ONELOCKURL("onelockURL");
		
		public String KEY;
		private OnlineSeat(String pkey){
			this.KEY = pkey;
		}
		public String getKey() {
	        return KEY;
	    }
		
		/**
		 * @description 
		 * 功能描述: 初始化选座配置文件
		 * @author 		  作         者: 张嘉杰
		 * @param	             参         数: propertiesKey  PageSize Key
		 * @createdate   建立日期：Jul 24, 20136:00:48 PM
		 */
		public static final String INIT(String propertiesKey){
			for (OnlineSeat o : OnlineSeat.values()) 
			{
				if (o.getKey().equals(propertiesKey)) 
				{
					String pkeyVal = OnlineSeatproperties.toMap().get(propertiesKey);
					if(StringUtil.isEmpty(pkeyVal))
					{
						log.warn("app.properties 未找到key为{}的参数!", propertiesKey);
					}
					return pkeyVal;
				}
		    }
			return null;
		}
		
		public static final Map<String,Object> callServiceJson(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			Map<String,Object> serviceMap = new HashMap<String, Object>();
			try {
				serviceMap= mapper.readValue(jsonStr, Map.class);
				serviceMap.put("jsonStr", jsonStr);
				log.info("服务器返回参数 ==>{}",serviceMap);
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return serviceMap;
			}
		}
		
		public static final SeatJudge callServiceJsonToSeatJudge(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatJudge seatJudge = new SeatJudge();
			try {
				seatJudge = mapper.readValue(jsonStr, SeatJudge.class);
				seatJudge.setJsonStr(jsonStr);
				log.info("服务器返回参数 ==>{}",seatJudge.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatJudge;
			}
		}
		
		public static final SeatResponse callServiceJsonToSeatResponse(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResponse onlineSeatResponse = new SeatResponse();
			try {
				onlineSeatResponse = mapper.readValue(jsonStr, SeatResponse.class);
				log.info("服务器返回参数 ==>{}",onlineSeatResponse.toString());
			} catch (Exception e) {
				//提示库存不足
				Map<String, Object> jsonMap = JsonUtils.parseMap(jsonStr);
				String result = "";
				if (!StringUtil.isEmpty(jsonMap.get("result"))) {
					result = jsonMap.get("result").toString();
				}
				onlineSeatResponse.setCode(200);
				onlineSeatResponse.setResult(null);
				onlineSeatResponse.setError(result);
				return onlineSeatResponse;
			}finally{
				return onlineSeatResponse;
			}
		}
		
		public static final SeatResult callServiceJsonToSeatResult(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(jsonStr, SeatResult.class);
				log.info("服务器返回参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatResult;
			}
		}
		
		public static final SeatResult jsonToSeatResult(String json){
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(json, SeatResult.class);
				log.info("json参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error(StringUtil.replace("jsonStr:{} ==> 解析失败...",json));
				return null;
			}finally{
				return seatResult;
			}
		}
		
	};
	/**
	 * @description 
	 * 功能描述: 获取选座地址
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 无
	 * @createdate   建立日期：Jul 24, 20136:00:48 PM
	 */
	public enum TheaterOnlineSeat {
		/** FALSH跳转地址  */
		FALSHURL("falshURL"),
		/** FALSH抢座跳转地址  */
		FALSHQZURL("falshQZURL"),
		/** 电子票特殊流程FALSH跳转地址  */
		FALSHDZURL("falshDZURl"),
		/** 电子票接口返回随机座位  */
		RANDOMSEATURL("randomSeatURL"),
		/** 选座服务器 获取场次URL  */
		SCREENINGSURL("screeningsURL"),
		/** 选座服务器 提交锁坐URL */
		LOCKSITURL("locksitURL"),
		/** 订单确认支付 最终锁死URL */
		SUCCESSURL("successURL"),
		/** 一次性锁座URL */
		ONELOCKURL("onelockURL");
		
		public String KEY;
		private TheaterOnlineSeat(String pkey){
			this.KEY = pkey;
		}
		public String getKey() {
	        return KEY;
	    }
		
		/**
		 * @description 
		 * 功能描述: 初始化选座配置文件
		 * @author 		  作         者: 张嘉杰
		 * @param	             参         数: propertiesKey  PageSize Key
		 * @createdate   建立日期：Jul 24, 20136:00:48 PM
		 */
		public static final String INIT(String propertiesKey){
			for (TheaterOnlineSeat o : TheaterOnlineSeat.values()) 
			{
				if (o.getKey().equals(propertiesKey)) 
				{
					String pkeyVal = TheaterOnlineSeatproperties.toMap().get(propertiesKey);
					if(StringUtil.isEmpty(pkeyVal))
					{
						log.warn("theater.properties 未找到key为{}的参数!", propertiesKey);
					}
					return pkeyVal;
				}
		    }
			return null;
		}
		
		public static final Map<String,Object> callServiceJson(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			Map<String,Object> serviceMap = new HashMap<String, Object>();
			try {
				serviceMap= mapper.readValue(jsonStr, Map.class);
				log.info("服务器返回参数 ==>{}",serviceMap);
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return serviceMap;
			}
		}
		
		public static final SeatJudge callServiceJsonToSeatJudge(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatJudge seatJudge = new SeatJudge();
			try {
				seatJudge = mapper.readValue(jsonStr, SeatJudge.class);
				log.info("服务器返回参数 ==>{}",seatJudge.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatJudge;
			}
		}
		
		public static final SeatResponse callServiceJsonToSeatResponse(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResponse onlineSeatResponse = new SeatResponse();
			try {
				onlineSeatResponse = mapper.readValue(jsonStr, SeatResponse.class);
				log.info("服务器返回参数 ==>{}",onlineSeatResponse.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return onlineSeatResponse;
			}
		}
		
		public static final SeatResult callServiceJsonToSeatResult(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(jsonStr, SeatResult.class);
				log.info("服务器返回参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatResult;
			}
		}
		
		public static final SeatResult jsonToSeatResult(String json){
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(json, SeatResult.class);
				log.info("json参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error("jsonStr:{} ==> 解析失败...",json);
				return null;
			}finally{
				return seatResult;
			}
		}
		
	};
	
	/**
	 * @description 
	 * 功能描述: 获取选座地址
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 无
	 * @createdate   建立日期：Jul 24, 20136:00:48 PM
	 */
	public enum KaixinguoOnlineSeat {
		/** FALSH跳转地址  */
		FALSHURL("falshURL");
		public String KEY;
		private KaixinguoOnlineSeat(String pkey){
			this.KEY = pkey;
		}
		public String getKey() {
	        return KEY;
	    }
		
		/**
		 * @description 
		 * 功能描述: 初始化选座配置文件
		 * @author 		  作         者: 张嘉杰
		 * @param	             参         数: propertiesKey  PageSize Key
		 * @createdate   建立日期：Jul 24, 20136:00:48 PM
		 */
		public static final String INIT(String propertiesKey){
			for (KaixinguoOnlineSeat o : KaixinguoOnlineSeat.values()) 
			{
				if (o.getKey().equals(propertiesKey)) 
				{
					String pkeyVal = KaixinguoOnlineSeatproperties.toMap().get(propertiesKey);
					if(StringUtil.isEmpty(pkeyVal))
					{
						log.warn("theater.properties 未找到key为{}的参数!", propertiesKey);
					}
					return pkeyVal;
				}
		    }
			return null;
		}
		
		public static final Map<String,Object> callServiceJson(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			Map<String,Object> serviceMap = new HashMap<String, Object>();
			try {
				serviceMap= mapper.readValue(jsonStr, Map.class);
				log.info("服务器返回参数 ==>{}",serviceMap);
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return serviceMap;
			}
		}
		
		public static final SeatJudge callServiceJsonToSeatJudge(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatJudge seatJudge = new SeatJudge();
			try {
				seatJudge = mapper.readValue(jsonStr, SeatJudge.class);
				log.info("服务器返回参数 ==>{}",seatJudge.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatJudge;
			}
		}
		
		public static final SeatResponse callServiceJsonToSeatResponse(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResponse onlineSeatResponse = new SeatResponse();
			try {
				onlineSeatResponse = mapper.readValue(jsonStr, SeatResponse.class);
				log.info("服务器返回参数 ==>{}",onlineSeatResponse.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return onlineSeatResponse;
			}
		}
		
		public static final SeatResult callServiceJsonToSeatResult(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(jsonStr, SeatResult.class);
				log.info("服务器返回参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return seatResult;
			}
		}
		
		public static final SeatResult jsonToSeatResult(String json){
			ObjectMapper mapper = new ObjectMapper();
			SeatResult seatResult = new SeatResult();
			try {
				seatResult = mapper.readValue(json, SeatResult.class);
				log.info("json参数 ==>{}",seatResult.toString());
			} catch (Exception e) {
				log.error("jsonStr:{} ==> 解析失败...",json);
				return null;
			}finally{
				return seatResult;
			}
		}
		
	};
		
	/**
	 * @description 
	 * 功能描述: 第三方电子票
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 无
	 * @createdate   建立日期：Jul 24, 20136:00:48 PM
	 */
	public enum DzSeat {
		/** 请求地址  */
		SENDURL("sendurl"),
		/** 发送短信 */
		SENDSMS("sendsms");
		//other...
		
		public String KEY;
		private DzSeat(String pkey){
			this.KEY = pkey;
		}
		public String getKey() {
	        return KEY;
	    }
		
		/**
		 * @description 
		 * 功能描述: 分页初始化
		 * @author 		  作         者: 张嘉杰
		 * @param	             参         数: propertiesKey  DzSeat Key
		 * @createdate   建立日期：Jul 24, 20136:00:48 PM
		 */
		public static final String INIT(String propertiesKey, String dzSeatId){
			for (DzSeat o : DzSeat.values()) 
			{
				if (o.getKey().equals(propertiesKey)) 
				{
					Map<String,String> mailMap = DzSeatproperties.toMapContainMap().get(dzSeatId);
					if(mailMap == null){
						log.error("dzseat.properties {}.{} 解析模板失败！",dzSeatId, propertiesKey);
					}
					return mailMap.get(propertiesKey);
				}
		    }
			return null;
		}
		
		public static final Map<String,Object> callServiceJson(String url){
			log.info("请求服务器地址 ==>{}",url);
			HtmlUtils html = new HtmlUtils();
			String jsonStr = html.getHtmlContent(url, true);
			ObjectMapper mapper = new ObjectMapper();
			Map<String,Object> serviceMap = new HashMap<String, Object>();
			try {
				serviceMap= mapper.readValue(jsonStr, Map.class);
				log.info("服务器返回参数 ==>{}",serviceMap);
			} catch (Exception e) {
				log.error("jsonStr:{},url:{} ==> 请求服务器失败...",jsonStr,url);
				return null;
			}finally{
				return serviceMap;
			}
		}
		
	}
    
}